package com.zjdiepu.www.rest.api.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.qiniu.common.QiniuException;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.file.IQiniuFileService;
import com.zjdiepu.www.model.base.request.QiniuKeyRequest;
import com.zjdiepu.www.model.base.request.QiniuRequest;
import com.zjdiepu.www.model.base.request.QiniuTokenRequest;

/**
 * 项目名称：dpqc-parent-manager-file <br/> 
 * 类名称：QiniuFileController <br/>
 * 类描述： 七牛文件处理<br/>
 * 创建人：winby <br/>
 * 创建时间：2017年4月26日 下午12:23:37  <br/>
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "QiniuFileController", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST})
@Api(tags = {"七牛文件操作"}, produces="application/json;charset=UTF-8")
public class QiniuFileController extends BaseController {
	
	@Autowired
	private IQiniuFileService qiniuFileService;
	
	/**
	 * @author winby <br/>
	 * @Title: getToken  <br/>
	 * @Description: 获取token <br/>
	 * @param
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "getToken")
	@ApiOperation(value = "获取上传授权token和文件名称", response = QiniuTokenRequest.class, httpMethod="POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
	public ApiResponse getToken(@Validated @RequestBody QiniuTokenRequest qiniuTokenRequest) {
		return ApiResponse.success(qiniuFileService.getToken(qiniuTokenRequest.getFileType(), null));
	}

	/**
	 * @author winby <br/>
	 * @Title: encodeVideo  <br/>
	 * @Description: 视频转码 <br/>
	 * @param
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "encodeVideo")
	@ApiOperation(value = "视频转码", response = QiniuRequest.class, httpMethod="POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
	public ApiResponse encodeVideo(@Validated(Add.class) @RequestBody QiniuRequest qiniuRequest) throws QiniuException {
		qiniuFileService.encodeMedia(qiniuRequest.getFileName(),qiniuRequest.getMediaType(), qiniuRequest.getAttachmentId());
		return ApiResponse.success(true);
	}

	/**
	 * @author winby <br/>
	 * @Title: encodeVideo  <br/>
	 * @Description: 删除文件 <br/>
	 * @param fileName
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "removeFile")
	@ApiOperation(value = "删除文件", response = Boolean.class, httpMethod="POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
	public ApiResponse removeFile(@Validated(Update.class) @RequestBody QiniuRequest qiniuRequest) {
		qiniuFileService.removeFile(qiniuRequest.getFileName());
		return ApiResponse.success(true);
	}
	/**
	 * @author winby <br/>
	 * @Title: getURL  <br/>
	 * @Description: 获取文件路径 <br/>
	 * @param key
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "getURL")
	@ApiOperation(value = "获取文件路径", response = QiniuKeyRequest.class, httpMethod="POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
	public ApiResponse getURL(@Validated @RequestBody QiniuKeyRequest qiniuKeyRequest) {
		Map<String, String> urls = CollectionInitUtil.emptyMap();
		String url = qiniuFileService.getURL(qiniuKeyRequest.getKey() + SystemConsts.THUMBNAIL_SUBBFIX);
		urls.put("url", url);
		if(qiniuKeyRequest.getBigImage() != null && qiniuKeyRequest.getBigImage()) {
			String bigUrl = qiniuFileService.getURL(qiniuKeyRequest.getKey());
			urls.put("bigUrl", bigUrl);
		}
		return ApiResponse.success(urls);
	}


	/**
	 * @param keys
	 * @return
	 * @author winby <br/>
	 * @Title: getURLs  <br/>
	 * @Description: 获取多个文件路径 <br/>
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "getURLs")
	@ApiOperation(value = "获取多个文件路径", response = QiniuKeyRequest.class, httpMethod="POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
	public ApiResponse getURLs(@Validated @RequestBody QiniuKeyRequest qiniuKeyRequest) {
		Map<String, String> urlsMap = CollectionInitUtil.emptyMap();
		String[] keys = StringUtils.split(qiniuKeyRequest.getKey(), ",");
		if(qiniuKeyRequest.getBigImage() == null || !qiniuKeyRequest.getBigImage()){
			Arrays.stream(keys).forEach(key -> urlsMap.put(key, qiniuFileService.getURL(key + SystemConsts.THUMBNAIL_SUBBFIX)));
		} else {
			Arrays.stream(keys).forEach(key -> urlsMap.put(key, qiniuFileService.getURL(key + SystemConsts.THUMBNAIL_SUBBFIX) + "," + qiniuFileService.getURL(key)));
		}
		return ApiResponse.success(urlsMap);
	}
}
